<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>用户管理</title>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#inputForm").validate({
				rules: {
					loginName: {remote: "${ctxAdmin}/sys/user/checkLoginName?oldLoginName=" + encodeURIComponent('${user.loginName}')}
				},
				messages: {
					loginName: {remote: "用户登录名已存在"},
					confirmNewPassword: {equalTo: "输入与上面相同的密码"}
				},
				submitHandler: function(form){
					cancelSelect();
					form.submit();
				},
				errorClass: 'validation-invalid-label',
				highlight: function(element, errorClass) {
					$(element).removeClass(errorClass);
				},
				errorPlacement: function (error, element) {
					validateErrorPlacement(error, element);
				}
			});
			layui.use(function(){
				var upload = layui.upload;
				var layer = layui.layer;
				var element = layui.element;
				var $ = layui.$;
				// 单图片上传
				var uploadInst = upload.render({
					elem: '#ID-upload-demo-choose'
					,url: ctxAdmin + '/sys/user/uploadIdPhoto?id=${user.id}'
					,acceptMime: 'image/png'
					,exts: 'png'
					,choose: function(obj){
						// 预读本地文件示例，不支持ie8
						obj.preview(function(index, file, result){
							$('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
						});
					}
					,done: function(res){
						// 若上传失败
						if(res.code > 0){
							return layer.msg('上传失败');
						}
						layer.msg('上传成功');
						// 上传成功的一些操作
						$('#ID-upload-demo-text').html('<span style="color: #5FB878;">上传成功</span>');
						// …
						$('#ID-upload-demo-text').html(''); // 置空上传失败的状态
					}
					,error: function(){
						// 演示失败状态，并实现重传
						var demoText = $('#ID-upload-demo-text');
						demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
						demoText.find('.demo-reload').on('click', function(){
							uploadInst.upload();
						});
					},
				});
			});
		});
	</script>
</head>
<body>
	<div class="card m-0 border-0">
		<sys:message content="${message}"/>
		<div class="card-body">
			<form:form id="inputForm" method="post" autocomplete="false" target="userFrame"
					   modelAttribute="user" action="${ctxAdmin}/sys/user/save" >
				<form:hidden path="id"/>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">一寸证件照</label>
					<div class="col-sm-9">
                        <div class="row">
                            <div class="col-sm-3">
                                <div class="layui-upload-list border-grey-300 border-1" style="width: 100px">
									<c:set var="tempPath" value="${ctxStatic}/assets/bootstrap/img/idPhoto.png"/>
									<c:set var="idPhotoPath" value="/upload/${user.photo}"/>
                                    <img class="layui-upload-img " id="ID-upload-demo-img" style="width: 100%; height: 140px;"
                                         src="${empty user.photo ? tempPath : idPhotoPath}">
                                    <div id="ID-upload-demo-text"></div>
                                </div>
                            </div>
                            <div class="col-sm-9">
								<div class="card border-0">
									<div class="card-body">
										<ul class="list list-unstyled mb-0">
											<li>
												<i class="mi-filter-1 text-primary"></i>
												尺寸：295*413(像素)
											</li>
											<li>
												<i class="mi-filter-2 text-primary "></i>
												格式：PNG格式
											</li>
											<li>
												<button type="button" class="btn btn-light mr-2" id="ID-upload-demo-choose">上传文件</button>
											</li>
										</ul>
									</div>
								</div>
                            </div>
                        </div>

					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">归属单位</label>
					<div class="col-sm-9">
						<sys:treeselect id="company" name="company.id" value="${user.company.id}" labelName="company.name" labelValue="${user.company.name}"
										title="公司" url="/sys/office/treeData?type=1" cssClass="required"/>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">归属部门</label>
					<div class="col-sm-9">
						<sys:treeselect id="office" name="office.id" value="${user.office.id}" labelName="office.name" labelValue="${user.office.name}"
										title="部门" url="/sys/office/treeData?type=2" cssClass="required" notAllowSelectParent="true"/>
					</div>
				</div>
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">用户姓名</label>
                    <div class="col-sm-9">
                        <form:input path="name" htmlEscape="false" maxlength="50" cssClass="form-control required" autocomplete="false"/>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">登录账号</label>
                    <div class="col-sm-9">
                        <input id="oldLoginName" name="oldLoginName" type="hidden" value="${user.loginName}">
                        <form:input path="loginName" htmlEscape="false" maxlength="50" cssClass="form-control required userName" autocomplete="false"/>
                    </div>
                </div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label"><c:if test="${empty user.id}"></c:if>登录密码</label>
					<div class="col-sm-9">
						<input id="newPassword" name="newPassword" type="password" maxlength="50" minlength="5" class="form-control ${empty user.id?'required':''}"/>
						<c:if test="${not empty user.id}"><span class="help-inline">若不修改密码，请留空。</span></c:if>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label"><c:if test="${empty user.id}"></c:if>确认密码</label>
					<div class="col-sm-9">
						<input id="confirmNewPassword" name="confirmNewPassword" type="password" maxlength="50" minlength="5" equalTo="#newPassword" class="form-control"/>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">证件类型</label>
					<div class="col-sm-9">
						<form:select path="credentialType" class="form-control required">
							<form:options items="${fns:getDictList('credential_type')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">证件编号</label>
					<div class="col-sm-9">
						<form:input path="credentialNo" htmlEscape="false" maxlength="100" cssClass="form-control required"/>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">学历</label>
					<div class="col-sm-9">
						<form:select path="educationalBackground" class="form-control required">
							<form:options items="${fns:getDictList('educational_background')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</div>
				</div>
				<%--<div class="form-group row">
					<label class="col-sm-3 col-form-label">职称</label>
					<div class="col-sm-9">
						<form:select path="professionalTitle" class="form-control required">
							<form:options items="${fns:getDictList('professional_title')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</div>
				</div>--%>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">用户性别</label>
					<div class="col-sm-9">
						<form:select path="sex" class="form-control required">
							<form:options items="${fns:getDictList('sex')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">电子邮箱</label>
					<div class="col-sm-9">
						<form:input path="email" htmlEscape="false" maxlength="100" cssClass="email form-control"/>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">电话号码</label>
					<div class="col-sm-9">
						<form:input path="phone" htmlEscape="false" maxlength="100" cssClass="form-control"/>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">手机号码</label>
					<div class="col-sm-9">
						<form:input path="mobile" htmlEscape="false" maxlength="100" cssClass="form-control"/>
					</div>
				</div>

				<div class="form-group row">
					<label class="col-sm-3 col-form-label">用户类型:</label>
					<div class="col-sm-9">
						<select name="userType" class="multiselect" multiple="multiple">
							<c:forEach items="${fns:getDictList('sys_user_type')}" var="dict" >
								<option value="${dict.value}" ${fn:contains(user.userType,dict.value) ? 'selected':''}>${dict.label}</option>
							</c:forEach>
						</select>
					</div>
				</div>
				<div class="form-group row">
					<label class="col-sm-3 col-form-label">用户角色</label>
					<div class="col-sm-9">
						<c:forEach items="${allRoles}" var="role" >
							<input type="checkbox" name="roleIdList" value="${role.id}"
								   <c:if test="${fn:contains(user.roleIdList,role.id)}">checked</c:if>
								   <c:if test="${role.id eq 6}">checked</c:if> >${role.name}
						</c:forEach>
					</div>
				</div>
				<c:if test="${not empty user.id}">
					<div class="form-group row">
						<label class="col-sm-3 col-form-label">允许登录</label>
						<div class="col-sm-9">
							<form:select path="loginFlag">
								<form:options items="${fns:getDictList('sys_yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
							</form:select>
						</div>
					</div>
				</c:if>
				<div class="form-group row mb-0">
					<div class="col-sm-9 ml-sm-auto">
						<shiro:hasPermission name="sys:user:edit">
							<button type="submit" class="btn btn-primary"><i class="icon-database-check mr-2"></i>保 存</button>
						</shiro:hasPermission>
						<button type="button" class="btn btn-light" onclick="cancelSelect()" ><i class="icon-database-arrow mr-2"></i>取 消</button>
					</div>
				</div>
			</form:form>
		</div>
	</div>
</body>
</html>